package com.ruoyi.mobile.api;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageSerializable;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.AjaxResults;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.qsng.domain.KIntegralDetailed;
import com.ruoyi.qsng.domain.KIntegralOrder;
import com.ruoyi.qsng.domain.KLearningHome;
import com.ruoyi.qsng.service.IKIntegralDetailedService;
import com.ruoyi.qsng.service.IKIntegralOrderService;
import com.ruoyi.qsng.service.IKLearningHomeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 学习之家控制器
 *
 * @author gk
 * @date 2023-06-14
 */
@Api(tags = "学习之家控制器")
@RestController
@RequestMapping
public class LearningHomeController  {

    @Autowired
    private IKLearningHomeService kLearningHomeService;

    @Autowired
    private IKIntegralOrderService kIntegralOrderService;

    @Autowired
    private IKIntegralDetailedService kIntegralDetailedService;

    @ApiOperation(value = "查询列表")
    @GetMapping("weixin/learningHome/list")
    public AjaxResults<List<KLearningHome>> list(
            @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(required = false, defaultValue = "10") Integer pageSize,
            KLearningHome kLearningHome)
    {
        PageHelper.startPage(pageNum, pageSize);
        List<KLearningHome> list = kLearningHomeService.selectKLearningHomeList(kLearningHome);
        return AjaxResults.success(new PageSerializable<>(list));
    }


    /**
     * 获取学习之家详细信息
     */
    @ApiOperation(value = "获取学习之家详细信息")
    @GetMapping(value = "/weixin/learningHome/{id}")
    public AjaxResults getInfo(@PathVariable("id") String id)
    {
        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        Assert.notNull(userId, "获取用户信息失败！");

        KLearningHome kLearningHome=kLearningHomeService.selectKLearningHomeById(id);
//        this.inserKIntegralOrder(userId,kLearningHome.getIntegral(),"浏览学习之家");
        return AjaxResults.success(kLearningHome);
    }


    public void inserKIntegralOrder(Long userId,Integer score,String name){
        //增加积分 先判断有无 有修改 无新增
        KIntegralOrder kIntegralOrder =new KIntegralOrder();
        kIntegralOrder.setUserId(String.valueOf(userId));
        List<KIntegralOrder> kIntegralOrderList=kIntegralOrderService.selectKIntegralOrderList(kIntegralOrder);
        if(kIntegralOrderList.size()>0){
            kIntegralOrder.setIntegral(kIntegralOrderList.get(0).getIntegral()+score);
            kIntegralOrder.setId(kIntegralOrderList.get(0).getId());
            kIntegralOrderService.updateKIntegralOrder(kIntegralOrder);
        }else {
            kIntegralOrder.setIntegral(score);
            kIntegralOrderService.insertKIntegralOrder(kIntegralOrder);
        }

        //增加记录
        KIntegralDetailed kIntegralDetailed =new KIntegralDetailed();
        kIntegralDetailed.setUserId(String.valueOf(userId));
        kIntegralDetailed.setIntegral(score);
        kIntegralDetailed.setType("105");
        kIntegralDetailed.setIntegralSource(name);
        kIntegralDetailedService.insertKIntegralDetailed(kIntegralDetailed);
    }

}
